package com.mingdao.lhy.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFUtils;
import com.linuxense.javadbf.DBFWriter;
import com.mingdao.lhy.pojo.XueJi;
import com.mingdao.lhy.pojo.signup.SignUpStudent2;

/**
 * https://blog.csdn.net/qazcxh/article/details/77019503
 * 
 * @author chenyj
 *
 */
public class DbfUtils {
	public static void main(String[] args) {
		DbfUtils dbfFileRAndW = new DbfUtils();
		try {
			dbfFileRAndW.testWriteAndReadAgain();
		} catch (DBFException | IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将已报名学生信息写入DBF文件
	 * 
	 * @param students
	 * @param filePath
	 * @throws DBFException
	 * @throws IOException
	 */
	public void writeSignUpStudent(List<SignUpStudent2> students, String filePath) throws DBFException, IOException {

		DBFField fields[] = new DBFField[25];

		fields[0] = new DBFField();
		fields[0].setName("wbxh");
		fields[0].setType(DBFDataType.CHARACTER);
		fields[0].setLength(10);

		fields[1] = new DBFField();
		fields[1].setName("bmh");
		fields[1].setType(DBFDataType.CHARACTER);
		fields[1].setLength(12);

		fields[2] = new DBFField();
		fields[2].setName("xm");
		fields[2].setType(DBFDataType.CHARACTER);
		fields[2].setLength(30);

		fields[3] = new DBFField();
		fields[3].setName("sfzh");
		fields[3].setType(DBFDataType.CHARACTER);
		fields[3].setLength(18);

		fields[4] = new DBFField();
		fields[4].setName("xjh");
		fields[4].setType(DBFDataType.CHARACTER);
		fields[4].setLength(19);

		fields[5] = new DBFField();
		fields[5].setName("byzxdm");
		fields[5].setType(DBFDataType.CHARACTER);
		fields[5].setLength(30);

		fields[6] = new DBFField();
		fields[6].setName("bynx");
		fields[6].setType(DBFDataType.CHARACTER);
		fields[6].setLength(4);

		fields[7] = new DBFField();
		fields[7].setName("xb");
		fields[7].setType(DBFDataType.CHARACTER);
		fields[7].setLength(4);

		fields[8] = new DBFField();
		fields[8].setName("csrq");
		fields[8].setType(DBFDataType.CHARACTER);
		fields[8].setLength(8);

		fields[9] = new DBFField();
		fields[9].setName("mz");
		fields[9].setType(DBFDataType.CHARACTER);
		fields[9].setLength(14);

		fields[10] = new DBFField();
		fields[10].setName("zzmm");
		fields[10].setType(DBFDataType.CHARACTER);
		fields[10].setLength(6);

		fields[11] = new DBFField();
		fields[11].setName("hklb");
		fields[11].setType(DBFDataType.CHARACTER);
		fields[11].setLength(6);

		fields[12] = new DBFField();
		fields[12].setName("hjd");
		fields[12].setType(DBFDataType.CHARACTER);
		fields[12].setLength(100);

		fields[13] = new DBFField();
		fields[13].setName("kslb");
		fields[13].setType(DBFDataType.CHARACTER);
		fields[13].setLength(10);

		fields[14] = new DBFField();
		fields[14].setName("lxdh");
		fields[14].setType(DBFDataType.CHARACTER);
		fields[14].setLength(11);

		fields[15] = new DBFField();
		fields[15].setName("yzbm");
		fields[15].setType(DBFDataType.CHARACTER);
		fields[15].setLength(6);

		fields[16] = new DBFField();
		fields[16].setName("txdz");
		fields[16].setType(DBFDataType.CHARACTER);
		fields[16].setLength(54);

		fields[17] = new DBFField();
		fields[17].setName("bjdm");
		fields[17].setType(DBFDataType.CHARACTER);
		fields[17].setLength(20);

		fields[18] = new DBFField();
		fields[18].setName("cszhlw");
		fields[18].setType(DBFDataType.CHARACTER);
		fields[18].setLength(2);

		fields[19] = new DBFField();
		fields[19].setName("js");
		fields[19].setType(DBFDataType.CHARACTER);
		fields[19].setLength(6);

		fields[20] = new DBFField();
		fields[20].setName("ddks");
		fields[20].setType(DBFDataType.CHARACTER);
		fields[20].setLength(6);

		fields[21] = new DBFField();
		fields[21].setName("xcqrf");
		fields[21].setType(DBFDataType.CHARACTER);
		fields[21].setLength(1);

		fields[22] = new DBFField();
		fields[22].setName("suiqian");
		fields[22].setType(DBFDataType.CHARACTER);
		fields[22].setLength(1);

		fields[23] = new DBFField();
		fields[23].setName("jdd");
		fields[23].setType(DBFDataType.CHARACTER);
		fields[23].setLength(6);

		fields[24] = new DBFField();
		fields[24].setName("dclzt");
		fields[24].setType(DBFDataType.CHARACTER);
		fields[24].setLength(1);

		DBFWriter writer = null;
		FileOutputStream fos = null;
		// File f = new File("/home/chenyj/tmp/ls18bmk.dbf");
		File f = new File(filePath);
		f.createNewFile();
		try {
			// 开始写
			fos = new FileOutputStream(f);
			writer = new DBFWriter(fos, java.nio.charset.Charset.forName("GBK"));
			// writer.setCharactersetName("UTF-8");
			// writer.setCharset(java.nio.charset.Charset.forName("utf8"));
			writer.setFields(fields);

			for (int i = 0; i < students.size(); i++) {
				SignUpStudent2 stu = students.get(i);
				Object rowData[] = new Object[25];
				rowData[0] = stu.getWbxh();
				rowData[1] = stu.getBmh();
				rowData[2] = stu.getXm();
				rowData[3] = stu.getSfzh();
				rowData[4] = stu.getXjh();
				rowData[5] = stu.getByzxdm();
				rowData[6] = stu.getBynx();
				rowData[7] = stu.getXb();
				rowData[8] = stu.getCsrq();
				rowData[9] = stu.getMz();
				rowData[10] = stu.getZzmm();
				rowData[11] = stu.getHklb();
				rowData[12] = stu.getHjd();
				rowData[13] = stu.getKslb();
				rowData[14] = stu.getLxdh();
				rowData[15] = stu.getYzbm();
				rowData[16] = stu.getTxdz();
				rowData[17] = stu.getBjdm();
				rowData[18] = stu.getCszhlw();
				rowData[19] = stu.getJs();
				rowData[20] = stu.getDdks();
				rowData[21] = stu.getXcqrf();
				rowData[22] = stu.getSuiqian();
				rowData[23] = stu.getJdd();
				rowData[24] = stu.getDclzt();
				writer.addRecord(rowData);
			}
			DBFUtils.close(writer);
			DBFUtils.close(fos);
			System.out.println("The dbf file product success!");

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}

	/**
	 * 将未报名学生信息写入DBF文件
	 * 
	 * @param students
	 * @param filePath
	 * @throws DBFException
	 * @throws IOException
	 */
	public void writeNotSignUpStudent(List<XueJi> students, String filePath) throws DBFException, IOException {

		DBFField fields[] = new DBFField[11];

		fields[0] = new DBFField();
		fields[0].setName("xjh");
		fields[0].setType(DBFDataType.CHARACTER);
		fields[0].setLength(19);

		fields[1] = new DBFField();
		fields[1].setName("xm");
		fields[1].setType(DBFDataType.CHARACTER);
		fields[1].setLength(54);

		fields[2] = new DBFField();
		fields[2].setName("sfzh");
		fields[2].setType(DBFDataType.CHARACTER);
		fields[2].setLength(18);

		fields[3] = new DBFField();
		fields[3].setName("bjdm");
		fields[3].setType(DBFDataType.CHARACTER);
		fields[3].setLength(20);

		fields[4] = new DBFField();
		fields[4].setName("xb");
		fields[4].setType(DBFDataType.CHARACTER);
		fields[4].setLength(1);

		fields[5] = new DBFField();
		fields[5].setName("mz");
		fields[5].setType(DBFDataType.CHARACTER);
		fields[5].setLength(2);

		fields[6] = new DBFField();
		fields[6].setName("csrq");
		fields[6].setType(DBFDataType.CHARACTER);
		fields[6].setLength(30);

		fields[7] = new DBFField();
		fields[7].setName("xszt");
		fields[7].setType(DBFDataType.CHARACTER);
		fields[7].setLength(20);

		fields[8] = new DBFField();
		fields[8].setName("wtlx");
		fields[8].setType(DBFDataType.CHARACTER);
		fields[8].setLength(50);

		fields[9] = new DBFField();
		fields[9].setName("cwlx");
		fields[9].setType(DBFDataType.CHARACTER);
		fields[9].setLength(50);

		fields[10] = new DBFField();
		fields[10].setName("byzxdm");
		fields[10].setType(DBFDataType.CHARACTER);
		fields[10].setLength(8);

		DBFWriter writer = null;
		FileOutputStream fos = null;

		// File f = new File("/home/chenyj/tmp/czxj.dbf");
		File f = new File(filePath);

		f.createNewFile();
		try {
			// 开始写
			fos = new FileOutputStream(f);
			writer = new DBFWriter(fos, java.nio.charset.Charset.forName("GBK"));
			// writer.setCharactersetName("UTF-8");
			// writer.setCharset(java.nio.charset.Charset.forName("utf8"));
			writer.setFields(fields);

			for (int i = 0; i < students.size(); i++) {
				XueJi stu = students.get(i);
				Object rowData[] = new Object[11];
				rowData[0] = stu.getXjh();
				rowData[1] = stu.getXm();
				rowData[2] = stu.getSfzh();
				rowData[3] = stu.getBjdm();
				rowData[4] = stu.getXb();
				rowData[5] = stu.getMz();
				rowData[6] = stu.getCsrq();
				rowData[7] = stu.getXszt();
				rowData[8] = stu.getWtlx();
				rowData[9] = stu.getCwlx();
				rowData[10] = stu.getByzxdm();

				writer.addRecord(rowData);
			}
			DBFUtils.close(writer);
			System.out.println("The dbf file product success!");

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}

	public void testWriteAndReadAgain() throws DBFException, IOException {
		// let us create field definitions first
		// we will go for 3 fields
		//
		DBFField fields[] = new DBFField[3];

		fields[0] = new DBFField();
		fields[0].setName("emp_code");
		fields[0].setType(DBFDataType.CHARACTER);
		fields[0].setLength(10);

		fields[1] = new DBFField();
		fields[1].setName("emp_name");
		fields[1].setType(DBFDataType.CHARACTER);
		fields[1].setLength(20);

		fields[2] = new DBFField();
		fields[2].setName("salary");
		fields[2].setType(DBFDataType.NUMERIC);
		fields[2].setLength(12);
		fields[2].setDecimalCount(2);
		DBFWriter writer = null;
		DBFReader reader = null;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		File f = new File("d:/emp.dbf");
		f.createNewFile();
		try {
			// 开始写
			fos = new FileOutputStream(f);
			writer = new DBFWriter(fos);
			writer.setFields(fields);

			// now populate DBFWriter
			//

			Object rowData[] = new Object[3];
			rowData[0] = "1000";
			rowData[1] = "John";
			rowData[2] = new Double(5000.00);

			writer.addRecord(rowData);

			rowData = new Object[3];
			rowData[0] = "1001";
			rowData[1] = "Lalit";
			rowData[2] = new Double(3400.00);

			writer.addRecord(rowData);

			rowData = new Object[3];
			rowData[0] = "1002";
			rowData[1] = "Rohit";
			rowData[2] = new Double(7350.00);

			writer.addRecord(rowData);

			DBFUtils.close(writer);
			System.out.println("The dbf file product success!");

			// 开始读
			fis = new FileInputStream(f);
			reader = new DBFReader(fis);
			Object[] objects = null;
			for (; (objects = reader.nextRecord()) != null;) {
				System.out.println(Arrays.toString(objects));
			}
			DBFUtils.close(reader);

		} finally {
			DBFUtils.close(reader);
			DBFUtils.close(writer);
		}
	}

}
